import path from "node:path";
import { defineConfig, loadEnv } from "vite";
import uni from "@dcloudio/vite-plugin-uni";
// @see https://github.com/uni-helper/vite-plugin-uni-components
import Components from "@uni-helper/vite-plugin-uni-components";

// https://vitejs.dev/config/
export default ({ command, mode }) => {
  const { UNI_PLATFORM } = process.env;
  const env = loadEnv(mode, path.resolve(process.cwd(), "env"));
  const { VITE_SHOW_SOURCEMAP, VITE_DELETE_CONSOLE } = env;

  return defineConfig({
    envDir: "./env",
    plugins: [
      uni(),
      // 自动安装 src/components 里面的组件为全局组件，非全局组件不要放到 src/components
      // Components({
      //   extensions: ["vue"],
      //   dirs: ["src/components"],
      //   dts: "src/types/components.d.ts",
      // }),
    ],
    server: {
      host: "0.0.0.0",
      hmr: true,
      port: 5173,
      // 仅 H5 端生效，其他端不生效（其他端走build，不走devServer)
      proxy: {
        // "/api/ibizplm__plmmob": {
        //   target: "http://172.16.220.130:30515",
        //   changeOrigin: true,
        // },
        "/api/demosys__mobvue3": {
          target: "http://172.16.103.169:30350",
          changeOrigin: true,
        },
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: '@import "@ibiz-template/theme/style/global.scss";',
        },
      },
    },
    define: {
      __UNI_PLATFORM__: JSON.stringify(UNI_PLATFORM),
    },
    optimizeDeps: {
      entries: [
        "@ibiz-template/core",
        "@ibiz-template/model-helper",
        "@ibiz-template/runtime",
        "@ibiz-template/theme",
        "@ibiz-template/mob-theme",
      ],
    },
    resolve: {
      alias: {
        "@": path.join(process.cwd(), "./src"),
        "@img": path.join(process.cwd(), "./src/static/images"),
      },
    },
    // build: {
    //   // 方便非h5端调试
    //   sourcemap: VITE_SHOW_SOURCEMAP === "true", // 默认是false
    //   target: "es6",
    //   // 开发环境不用压缩
    //   minify: mode === "development" ? false : "terser",
    //   terserOptions: {
    //     compress: {
    //       drop_console: VITE_DELETE_CONSOLE === "true",
    //       drop_debugger: true,
    //     },
    //   },
    // },
  });
};
